package com.zzxx.exam.entity;

import com.zzxx.exam.util.Config;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 实体数据管理, 用来读取数据文件放到内存集合当中
 */
public class EntityContext {
    public EntityContext() throws IOException {
        userReader();
        questionReader();
        rulesReader();
    }

    /**
     * 解析配置文件信息：
     *
     *  用户解析
     *
     *  试卷解析
     *
     *  考试规则解析
     */

    public User getUserById(Integer id) {
        return userMap.get(id);
    }

    public List<Question> getQuestionsById(int level) {
        return new ArrayList<>(questionsMap.get(level));
    }

    public String getPaperTitle() {
        return "这是一场考试";
    }

    public int getTimeLimit() {
        return 10;
    }

    public String getMsg() {
        return examRules.toString();
    }

    /**定义用户信息集合为成员变量-------------------用户解析----------------------------------------------------------------*/
    private ArrayList<User> userList;
    private HashMap<Integer,User> userMap;
    @Test /**用户信息读取入集合*/
    public void userReader() throws IOException {
        /**读取账户信息的文件到流中*/
        String path = EntityContext.class.getClassLoader().getResource("com/zzxx/exam/util/user.txt").getPath();
        //System.out.println(path);
        BufferedReader userInfo = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

        /**将流中信息封装进user对象*/
        userList = new ArrayList<>();
        userMap = new HashMap<>();
        String userLine;
        while ((userLine = userInfo.readLine()) != null){
            userLine = userLine.trim();
            if (userLine.startsWith("#") || userLine.equals("")){
                continue;
            }
            User user = new User();
            String[] userStr = userLine.split(":");

            user.setId(Integer.parseInt(userStr[0]));
            user.setName(userStr[1]);
            user.setPassword(userStr[2]);
            user.setPhone(userStr[3]);
            user.setEmail(userStr[4]);
            userMap.put(Integer.parseInt(userStr[0]),user);
            userList.add(user);
        }
        //System.out.println(userList);
    }


    /**定义试题集合为成员变量---------------试题解析-----------------------------------------------------------------------*/
    private ArrayList<Question> questions;
    private HashMap<Integer,ArrayList<Question>> questionsMap;
    @Test /**考试题读取入集合*/
    public void questionReader() throws IOException {
        String path = EntityContext.class.getClassLoader().getResource("com/zzxx/exam/util/corejava.txt").getPath();
        //System.out.println(path);
        BufferedReader questionObj = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

        questions = new ArrayList<>();
        questionsMap = new HashMap<>();
        String questionLine;
        int count = 0;
        while ((questionLine = questionObj.readLine()) != null){
            if (questionLine.startsWith("@")) {
                /**新建选项和正确答案的集合用于临时存储*/
                ArrayList<String> options = new ArrayList<>();
                ArrayList<Integer> answers = new ArrayList<>();
                /**新建当前question对象*/
                Question question = new Question();
                /**设置ID 并存入question*/
                question.setId(count);
                count++;
                /**切割第一行 第一行包含正确答案、分数、等级*/
                String[] split = questionLine.split(",");
                /**截取正确答案 并存入question对象*/
                String ans = split[0].substring(split[0].indexOf("=") + 1);
                for (String i : ans.split("/")) {
                    answers.add(Integer.valueOf(i));
                }
                question.setAnswers(answers);
                /**设置question题目是否为多选题*/
                if (answers.size() == 1){
                    question.setType(Question.SINGLE_SELECTION);
                }else {
                    question.setType(Question.MULTI_SELECTION);
                }
                /**截取分数 并存入question对象*/
                question.setScore(Integer.parseInt(split[1].substring(split[1].indexOf("=")+1)));
                /**截取等级 并存入question对象*/
                question.setLevel(Integer.parseInt(split[2].substring(split[2].indexOf("=")+1)));
                /**截取题干 并存入question对象*/
                question.setTitle(questionObj.readLine());
                /**截取四个选项 并存入question对象*/
                for (int i = 0; i < 4; i++) {
                    options.add(questionObj.readLine());
                }
                question.setOptions(options);
                /**在questions集合中添加该题*/
                questions.add(question);
            }
        }
        /**分难度等级存入Map*/
        for (Question qus : questions){
            classification(qus.getLevel(),qus);
        }
    }
    /**难度分类具体实现*/
    private void classification(int level, Question qus) {
        if (questionsMap.get(level) == null){
            ArrayList<Question> questionClass = new ArrayList<>();
            questionsMap.put(level,questionClass);
            questionsMap.get(level).add(qus);
        }else {
            questionsMap.get(level).add(qus);
        }
    }

    /**解析考试规则----------------------考试规则解析---------------------------------------------------------------------*/
    private ArrayList<String> examRules;
    @Test
    public void rulesReader() throws IOException {
        String path = EntityContext.class.getClassLoader().getResource("com/zzxx/exam/util/rule.txt").getPath();
        //System.out.println(path);
        BufferedReader ruleInfo = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

        examRules = new ArrayList<>();
        String ruleLine;
        String regex = "\\d+\\..+";
        while ((ruleLine = ruleInfo.readLine()) != null){
            String line = ruleLine.trim();
            if (line.matches(regex)){
                examRules.add(line.replaceAll(" ","") + "\n");
            }
        }
        //System.out.println(examRules);
    }

    private QuestionInfo questionInfo;
    private ExamInfo examInfo;




    /**get和set方法*/
    public ArrayList<User> getUserList() {
        return userList;
    }

    public HashMap<Integer, User> getUserMap() {
        return userMap;
    }

    public ArrayList<Question> getQuestions() {
        return questions;
    }

    public ArrayList<String> getExamRules() {
        return examRules;
    }

    public HashMap<Integer, ArrayList<Question>> getQuestionsMap() {
        return questionsMap;
    }



//    public static void main(String[] args) throws IOException {
//        EntityContext entityContext = new EntityContext();
//        entityContext.questionReader();
//        System.out.println(entityContext.getQuestionsMap());
//    }
}
